package com.yoyo.lib.net;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.yoyo.lib.net.itf.IProgressListener;
import com.yoyo.lib.net.itf.ICallback;
import com.yoyo.lib.net.itf.IRequestListener;
import com.yoyo.lib.utilities.Trace;
import com.yoyo.lib.utilities.UrlHelper;

public class RequestInformation {

    public String url;
    public HashMap<String, String> headers;
    public String mRequestMethod = "GET";
    public ArrayList<NameValuePair> urlParameters;
    public String postContent;
    public ICallback requestCallback;
    public IRequestListener mRequestListener;
    public IProgressListener mProgressChangeListener;
    public String encoding = UrlHelper.DEFAULT_ENCODING;
    private RequestTask requestTask;
    public byte[] byteParams;
    public final static String REQUEST_METHOD_GET = "GET";
    public final static String REQUEST_METHOD_POST = "POST";

    public RequestInformation(String url, String method) {
        Trace.d(url);
        this.url = url;
        this.mRequestMethod = method;
    }

    public RequestInformation(String url, String method, ArrayList<NameValuePair> forms) {
        Trace.d(url);
        this.url = url;
        this.mRequestMethod = method;
        this.urlParameters = forms;
    }

    public void addHeader(String key, String value) {
        if (headers == null) {
            headers = new HashMap<String, String>();
        }
        headers.put(key, value);
    }

    public void addPostParams(String key, String value) {
        if (urlParameters == null) {
            urlParameters = new ArrayList<NameValuePair>();
        }
        urlParameters.add(new BasicNameValuePair(key, value));
    }

    public void execute() {
        requestTask = new RequestTask(this);
        requestTask.execute();
    }

    public void cancel() {
        if (requestTask != null) {
            requestCallback.cancel();
            requestTask.cancel(true);
        }
    }

    public void setCallback(ICallback callback) {
        this.requestCallback = callback;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public void setRequestListener(IRequestListener listener) {
        this.mRequestListener = listener;
    }

    public void setProgressChangeListener(IProgressListener mProgressChangeListener) {
        this.mProgressChangeListener = mProgressChangeListener;
    }

}